<template>
  <div v-show="$route.name == 'material-in'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>原料入库明细</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <div style="margin-bottom: 20px;">原料入库明细</div>
      <el-row>
        <el-col :span="24">
          <el-button type="warning" @click="exportExcel">导出至excel</el-button>
        </el-col>
      </el-row>
      <el-table
        border
        v-loading="loading"
        :data="materialInData"
        style="margin-top: 10px;">
        <el-table-column label="日期" prop="default_time" min-width="110" align="center" fixed="left">
          <template slot-scope="scope">
            <span>{{ new Date(scope.row.default_time).toLocaleDateString().split('/').join('-') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采购单号" prop="house_in_number" width="130" align="center" fixed="left"></el-table-column>
        <el-table-column label="分类" prop="category_name" min-width="110" align="center" fixed="left"></el-table-column>
        <el-table-column label="商品名称" prop="name" min-width="160" align="center" fixed="left"></el-table-column>
        <el-table-column label="采购信息" min-width="130" align="center">
          <el-table-column label="采购数量" prop="count_in" width="90" align="center"></el-table-column>
          <el-table-column label="单位" prop="unit_in" width="70" align="center"></el-table-column>
          <el-table-column label="成本" prop="price_in" min-width="100" align="center" v-if="groupid === 0 || groupid === 2">
            <template slot-scope="scope">
              <span>{{ parseFloat(scope.row.price_in).toFixed(2) }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="转换比例" prop="conversion" min-width="100" align="center">
          <template slot-scope="scope">
            <span>{{ parseFloat(scope.row.conversion).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入库信息" min-width="130" align="center">
          <el-table-column label="仓库数量" prop="count" width="90" align="center"></el-table-column>
          <el-table-column label="单位" prop="unit" width="70" align="center"></el-table-column>
          <el-table-column label="成本" prop="price" min-width="100" align="center" v-if="groupid === 0 || groupid === 2">
            <template slot-scope="scope">
              <span>{{ parseFloat(scope.row.price).toFixed(2) }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="总成本" prop="total_amount" min-width="120" align="center" v-if="groupid === 0 || groupid === 2">
          <template slot-scope="scope">
            <span>{{ parseFloat(scope.row.total_amount).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商" prop="supplier" min-width="120" align="center"></el-table-column>
        <el-table-column label="备注" prop="comment" min-width="180" align="center"></el-table-column>
        <el-table-column label="操作" width="80" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="deleteHouseIn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      groupid: '',
      loading: false,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      materialInData: []
    }
  },
  mounted() {
    this.getUserInfo()
    this.getHouseInDetailList()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      const { data: result } = await this.$http.get('/user/' + localStorage.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.groupid = result.data.groupid * 1
    },
    async getHouseInDetailList() {
      this.loading = true
      const params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize,
        process: 0,
        status: 1
      }
      const { data: result } = await this.$http.get('/house-in/detail', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.materialInData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.materialInData = result.data
      this.pager = result.pager
    },
    // 导出
    async exportExcel() {
      this.loading = true
      const data = {
        process: 0,
        status: 1,
        groupid: this.groupid
      }
      await this.$http.post('/house-in/export', data, { type: 'json',responseType:'blob' }).then(res => {
        if (res.status === 200) {
          const blob = res.data
          const reader = new FileReader()
          reader.readAsDataURL(blob)
          reader.onload = (e) => {
            const a = document.createElement('a')
            a.download = '采购明细.xlsx'
            a.href = e.target.result
            a.click()
          }
          this.loading = false
          return this.$message.success('导出成功')
        }
        this.loading = false
        return this.$message.error(res.msg)
      }).catch (() => {
        this.loading = false
        return this.$message.error('导出失败')
      })
    },
    // 删除信息
    async deleteHouseIn(id) {
      const { data: result } = await this.$http.delete('/house-in/detail/' + id)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.pager.currPage = 1
      this.getHouseInDetailList()
      this.$message.success('删除成功')
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getHouseInDetailList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getHouseInDetailList()
    }
  }
}
</script>